{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "43e38f53",
   "metadata": {},
   "source": [
    "___________________\n",
    "\n",
    "* 时间：2022/11/21/week14\n",
    "* python学习记录\n",
    "____________________\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80232eac",
   "metadata": {},
   "source": [
    "# 本周目标\n",
    "\n",
    "> 1. 函数的定义\n",
    "> 2. 如何使用函数\n",
    "> 3. 函数的返回值\n",
    "> 4. 使用注释改进文档\n",
    "> 5. 传递任意数量的实参\n",
    "> 6. 函数与模块\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7052e34",
   "metadata": {},
   "source": [
    "## 1、函数的定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "abef6698",
   "metadata": {},
   "outputs": [],
   "source": [
    "def greet_user():\n",
    "    \"\"\"对某人的问候语\"\"\"\n",
    "    print(\"hello world\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4a6d8ef1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "greet_user()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cacae093",
   "metadata": {},
   "outputs": [],
   "source": [
    "def greet_user(username):\n",
    "    \"\"\"对某人的问候语\"\"\"\n",
    "    print(\"hello\" +username+\"~\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6659833d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hellolinhan~\n"
     ]
    }
   ],
   "source": [
    "greet_user(\"linhan\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "380e6ed4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    \"\"\"y=2x的函数计算\"\"\"\n",
    "    print(2*x)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b18fa014",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n"
     ]
    }
   ],
   "source": [
    "f(9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f0398457",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践 编程思维（计算思维）下，函数的应用\n",
    "def nfu_news_url(类别,page):\n",
    "    for page in range(page):\n",
    "        print(\"https://www.nfu.edu.cn/\"+ 类别 +\"/index\" + str(page) + \".htm\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7dccd791",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.nfu.edu.cn/tzgg/index0.htm\n",
      "https://www.nfu.edu.cn/tzgg/index1.htm\n",
      "https://www.nfu.edu.cn/tzgg/index2.htm\n",
      "https://www.nfu.edu.cn/tzgg/index3.htm\n",
      "https://www.nfu.edu.cn/tzgg/index4.htm\n",
      "https://www.nfu.edu.cn/tzgg/index5.htm\n",
      "https://www.nfu.edu.cn/tzgg/index6.htm\n",
      "https://www.nfu.edu.cn/tzgg/index7.htm\n",
      "https://www.nfu.edu.cn/tzgg/index8.htm\n",
      "https://www.nfu.edu.cn/tzgg/index9.htm\n"
     ]
    }
   ],
   "source": [
    "nfu_news_url(\"tzgg\",10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d8a508e",
   "metadata": {},
   "source": [
    "## 2. 如何使用函数（传递实参）\n",
    "> 1. positional argument(位置参数)\n",
    "> 2. keyword argument(关键字参数)  #关键词参数是不能写在位置参数前面的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "0a1460d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 通过案例来思考：位置参数 VS 关键字参数\n",
    "def f(x,y):\n",
    "    print(x)\n",
    "    print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "4a9542a4",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (3059410298.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [19]\u001b[1;36m\u001b[0m\n\u001b[1;33m    f(x=3,1)\u001b[0m\n\u001b[1;37m           ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "f(x=3,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "3cc3fa87",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(3,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8ac4a969",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(x=3,y=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "32bc1632",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(3,y=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "896bdd89",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x,y,z):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "4e32f290",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "2\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "f(3,2,5) # 根据上面的经验，这个位置应该是位置参数吧（领悟）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "8613bd26",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "10\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "f(y=10,x=3,z=7)# 这个应该是关键字参数（领悟），关键字参数考虑顺序吗？（不考虑顺序）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "3f00cd76",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "9\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "f(3,z=10,y=9)# ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "10605b4f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "10\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "f(3,y=10,z=8)# ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "c56b79de",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (1058910829.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [28]\u001b[1;36m\u001b[0m\n\u001b[1;33m    f(3,y=9,10)# ?\u001b[0m\n\u001b[1;37m              ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "f(3,y=9,10)# ?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08dc0cd7",
   "metadata": {},
   "source": [
    "> 3.默认参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "479fe7ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a hamster.\n",
      "Myhamster's name is Harry.\n",
      "\n",
      "I have a cat.\n",
      "Mycat's name is Python.\n"
     ]
    }
   ],
   "source": [
    "def describe_pet(pet_name,animal_type=\"dog\",):\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a \" + animal_type + \".\")\n",
    "    print(\"My\" + animal_type + \"'s name is \"+pet_name.title() + \".\")\n",
    "\n",
    "describe_pet(pet_name='harry',animal_type='hamster')  #关键字实参\n",
    "describe_pet(pet_name='python',animal_type='cat')  #关键字实参"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "a614017a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a hamster.\n",
      "Myhamster's name is Harry.\n",
      "\n",
      "I have a cat.\n",
      "Mycat's name is Python.\n",
      "\n",
      "I have a dog.\n",
      "Mydog's name is Jerry.\n"
     ]
    }
   ],
   "source": [
    "def describe_pet(pet_name,animal_type=\"dog\",):\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a \" + animal_type + \".\")\n",
    "    print(\"My\" + animal_type + \"'s name is \"+pet_name.title() + \".\")\n",
    "\n",
    "describe_pet(pet_name='harry',animal_type='hamster')  #关键字实参\n",
    "describe_pet(pet_name='python',animal_type='cat')  #关键字实参\n",
    "describe_pet('Jerry')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "e5a45abd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "Mydog's name is Jerry.\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "print(describe_pet('Jerry'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "fd1dc53f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function print in module builtins:\n",
      "\n",
      "print(...)\n",
      "    print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n",
      "    \n",
      "    Prints the values to a stream, or to sys.stdout by default.\n",
      "    Optional keyword arguments:\n",
      "    file:  a file-like object (stream); defaults to the current sys.stdout.\n",
      "    sep:   string inserted between values, default a space.\n",
      "    end:   string appended after the last value, default a newline.\n",
      "    flush: whether to forcibly flush the stream.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(print)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "85570317",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello|||||world|||||你好|||||世界\n",
      "hello world 你好 世界\n"
     ]
    }
   ],
   "source": [
    "print(\"hello\",\"world\",\"你好\",\"世界\",sep='|||||')\n",
    "print(\"hello\",\"world\",\"你好\",\"世界\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "4c5d5863",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function describe_pet in module __main__:\n",
      "\n",
      "describe_pet(pet_name, animal_type='dog')\n",
      "    显示宠物信息\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(describe_pet)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a04aa0ea",
   "metadata": {},
   "source": [
    "## 3. 函数的返回值\n",
    "> 1. 几乎所有编程语言，函数是要求必须有返回值！\n",
    "> 2. Python作为高级语言，可以不写返回值，但是一般情况下，也是要写的，不写的话默认返回 None\n",
    "> 3. 函数的返回值：return\n",
    ">> 3.1 return 可以返回过程运算的结果\n",
    ">> 3.2 return 可以返回过程自定义结果（由程序开发人员/设计人员定制的） \n",
    "\n",
    "## 4. 函数的优势\n",
    "* 1. 可重复使用\n",
    "* 2. 具有强大的可扩展性\n",
    "* 3. 具有可定制化性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "892e5c42",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    y = x+2\n",
    "    print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "9e7eedc5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "f(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "8692afda",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'y' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [45]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43my\u001b[49m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'y' is not defined"
     ]
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "f4a5d513",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    y = x+2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "7d6e3207",
   "metadata": {},
   "outputs": [],
   "source": [
    "y = f(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "1aec490f",
   "metadata": {},
   "outputs": [],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "ac7856cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    y = x+2\n",
    "    return y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "f1296e53",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = f(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "25a178d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "884ded2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    y = x+2\n",
    "    return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "86fda180",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = f(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "f7cbb714",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "d54c481f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    y = x+2\n",
    "    return y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "49776ccf",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = f(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "ec30a071",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13a1f1cb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
